<template>
<h2>会员信息填写</h2>
<table class="table table-bordered">
    <tr>
        <td>会员名称</td>
        <td>
            <input type="text" v-model="info.mname"/>
        </td>
    </tr>
    <tr>
        <td>会员电话</td>
        <td>
            <input type="text" v-model="info.phone"/>
        </td>
    </tr>
    <tr>
        <td>会员性别</td>
        <td>
            <input type="radio" :value="'男'" v-model="info.sex"/>男
            <input type="radio" :value="'女'" v-model="info.sex"/>女
        </td>
    </tr>
    <tr>
        <td>会员生日</td>
        <td>
            <input type="date" v-model="info.birenday"/>
        </td>
    </tr>
    <tr>
        <td>会员余额</td>
        <td>
            <input type="text" v-model="info.ymoney"/>
        </td>
    </tr>
    <tr>
        <td>会员积分</td>
        <td>
            <input type="text" v-model="info.jifen"/>
        </td>
    </tr>
    <tr>
        <td>会员头像</td>
        <td>
            <img :src="info.image" style="width: 70px;height: 70px;"/>
            <input type="file" @change="upload"/>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" value="添加" class="btn btn-info" @click="Add" />
        </td>
    </tr>
</table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router';
const router=useRouter()

const info=ref({
    mname:'',
    phone:'',
    sex:'男',
    birenday:'',
    ymoney:0,
    jifen:0,
    image:''
})

//上传图片
const file=ref('')
const upload=(e:any)=>{
    file.value=e.target.files[0]
    if(!file.value){
        alert('请选择图片')
        return
    }
    const formdata=new FormData()
    formdata.append('file',file.value)
    axios.post('https://localhost:7279/api/File/UpLoad',formdata)
    .then(res=>{
        if(res.data.code==1){
            alert(res.data.message)
        }
        else if(res.data.code==2){
            alert(res.data.message)
        }
        else if(res.data.code==3){
            alert(res.data.message)
            info.value.image=res.data.path
        }
        else{
            alert('上传失败')
        }
    })
    .catch(err=>{
        console.log(err)
    })
}

//添加
const Add=()=>{
    var obj={
        mname:info.value.mname,
        phone:info.value.phone,
        sex:info.value.sex,
        birenday:info.value.birenday,
        ymoney:info.value.ymoney,
        jifen:info.value.jifen,
        image:info.value.image
    }
    if(!obj.mname){
        alert('会员名称不能为空')
        return
    }
    if(!obj.phone){
        alert('手机号码不能为空')
        return
    }
    if(!obj.birenday){
        alert('会员生日不能为空')
        return
    }
    if(!obj.image){
        alert('头像必须上传')
        return
    }
    axios.post('https://localhost:7279/api/Meber/MeberAdd',obj)
    .then(res=>{
        if(res.data==-1){
            alert('会员名称不能重复')
        }
        else if(res.data>0){
            alert('填写完成')
            router.push('MeberShow')
        }
        else{
            alert('填写失败')
        }
    })
    .catch(err=>{
        console.log(err)
    })
}


</script>